home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility3 / huge2.zip / HUGEARR.TXT < prev    next >
Text File  |  1992-02-23  |  17KB  |  522 lines

  1.                                 HUGEARR.DLL
  2.                Huge array support for Microsoft Visual Basic
  3.  
  4.               from Microsoft Product Support Services 6/4/91
  5.  
  6. The following information applies to Microsoft Visual Basic
  7. programming system version 1.0 for Microsoft Windows, and to Microsoft
  8. Windows 3.0 Software Development Kit (SDK).
  9.  
  10. HUGEARR.DLL is a dynamic-link library (DLL) which contains functions
  11. for creation, maintenance, and deletion of arrays larger than 64K from
  12. Microsoft Visual Basic version 1.00 for Windows. This DLL also gives
  13. the ability to create arrays with more than 32,767 (32K) elements per
  14. dimension, and to redimension arrays while preserving the data inside
  15. of the arrays.
  16.  
  17. The following files are provided:
  18.  
  19.    HUGEARR.DLL, HUGEARR.BAS, HUGEARR.C, HUGEARR.DEF, HUGEARR.H,
  20.    HUGEARR.TXT, MAKEFILE
  21.  
  22. To use the functions in HUGEARR.DLL, simply copy the declarations
  23. contained in HUGEARR.BAS into your global module in Visual Basic and
  24. copy HUGEARR.DLL to your Windows directory. The functions can then be
  25. used like any other Windows DLL function.
  26.  
  27. HUGEARR.DLL allocates memory using the Windows API function
  28. GlobalAlloc. This means that the largest array that can be allocated
  29. is 1 MB in standard mode, and 64 MB in 386 enhanced mode for Windows.
  30.  
  31. The following routines are contained in HUGEARR.DLL. For a complete
  32. description of the parameters and/or return values of these routines,
  33. see Visual Basic's Declare statement for the routine in question in
  34. the file HUGEARR.BAS.
  35.  
  36. HUGEARR.DLL Language Reference:
  37. ------------------------------
  38.  
  39. -------------------------------------------------------------------------
  40. HugeDim:
  41.  
  42. Action: Dimensions an array and returns a handle to that array.
  43.  
  44. Syntax: HugeDim(recsize%, limit&)
  45.  
  46. Argument       Description
  47. --------       -----------
  48.  
  49. recsize%       The size of each element in the array.  (i.e. an integer
  50.                would be 2, a double would be 8.)  You can use the Len()
  51.                function to determine the size of any data type if you are
  52.                unsure.
  53.  
  54. limit&         The upper bound of the array.  The lower bound of all arrays
  55.                is 0, so for example: 'HugeDim(2, 10)' would create an
  56.                integer array of elements 0 through 10.
  57.  
  58. Remarks:
  59. -------
  60.  
  61. You should not try to create a huge array of variable-length strings, or of
  62. a user-defined type that contains variable-length strings.  Visual Basic's
  63. string handling routines would not know of the existence of any string that
  64. was controlled by HUGEARR.DLL, and a UAE ("Unrecoverable Application Error")
  65. would probably result.  Fixed-length strings are okay though.
  66.  
  67. If the total size (in bytes) of the array is going to be bigger than 64K,
  68. the size of each element has to be an integer power of two (1, 2, 4, 8,
  69. and so forth.)  This is necessary so that the no element of an array
  70. straddles a segment boundary.
  71.  
  72. HugeDim returns a handle to the array that was created, and that handle is
  73. used when referring to that array with other commands.  If an error
  74. occurred (such as out of memory), it will return a negative number.  Error
  75. codes are detailed below under the section entitled 'Error Codes'.
  76.  
  77. Example:
  78. -------
  79.  
  80. Sub Command1_Click()
  81.      Dim hArray as integer
  82.      Dim variable as SomeUserDefinedType
  83.  
  84.      hArray = HugeDim(Len(variable), 10)
  85.      If hArray < 0 Then
  86.           print "Error dimensioning array:"; hArray
  87.           Stop
  88.      End If
  89.           .
  90.           .
  91.           .
  92.      i% = HugeErase(hArray)
  93. End Sub
  94.  
  95. -------------------------------------------------------------------------
  96. HugeErase:
  97.  
  98. Action: Erases an array that was previously dimensioned using HugeDim.
  99.  
  100. Syntax: HugeErase(hArray%)
  101.  
  102. Argument       Description
  103. --------       -----------
  104.  
  105. hArray%        The handle to the array.  (The same handle that was returned
  106.                by HugeDim.)
  107.  
  108. Remarks:
  109. --------
  110.  
  111. You should be sure to HugeErase all arrays that were HugeDim'd.  Failing to
  112. do so would cause the memory used by the array to not be freed up until the
  113. application quits.  If many arrays are dimensioned but never erased memory
  114. would keep being allocated without being freed, eventually degrading system
  115. performance.
  116.  
  117. Example:  Refer to the example for HugeDim.
  118.  
  119. -------------------------------------------------------------------------
  120. HugeRedim:
  121.  
  122. Action:  Redimensions an array created with HugeDim to a different size.
  123.  
  124. Syntax:  HugeRedim(hArray%, limit&)
  125.  
  126. Argument       Description
  127. --------       -----------
  128.  
  129. hArray%        The handle to the array to redimension.
  130.  
  131. limit&         The new upper bound of the array.
  132.  
  133. Remarks:
  134. --------
  135.  
  136. HugeRedim, unlike Visual Basic's ReDim, preserves all the data in the
  137. array.  If you want to erase the contents of the array, you should erase it
  138. and the dimension it again.
  139.  
  140. If the size of the array would go over 64K when it is redimensioned, it is
  141. subject to the same size restrictions detailed in the discussion of
  142. HugeDim.
  143.  
  144. You cannot change the size of the elements in the array, only the number of
  145. elements.
  146.  
  147. Example:
  148. --------
  149.  
  150. Sub Command1_Click()
  151.      .
  152.      .
  153.      i% = HugeRedim(hArray, 50)
  154.      if i% < 0 then
  155.           print "error redimensioning array:"; hArray
  156.           stop
  157.      End If
  158.  
  159.      e% = HugeErase(hArray)
  160. End Sub
  161.  
  162. -------------------------------------------------------------------------
  163. GetHugeEl, SetHugeEl:
  164.  
  165. Action:  Gets or sets the contents of an array element.
  166.  
  167. Syntax:  GetHugeEl(hArray%, el&, variable)
  168.         SetHugeEl(hArray%, el&, variable)
  169.  
  170. Argument       Description
  171. --------       -----------
  172.  
  173. hArray%        The handle to the array.
  174.  
  175. el&            The element of the array to set or get the data from.
  176.  
  177. variable       The variable to get into, or to set the array from.
  178.  
  179. Remarks:
  180. --------
  181.  
  182. It is extremely important that the type for the variable passed to
  183. GetHugeEl or SetHugeEl matches that type of the variable used in the
  184. HugeDim statement, if the types are of different lengths, you will mostly
  185. likely get a UAE or overwrite other data.  If you want to be sure that the
  186. types match you can use the Alias keyword when you declare the function to
  187. introduce type checking.  Refer to the section below entitled 'Aliasing'
  188. for more information on aliases.
  189.  
  190. Example:
  191. --------
  192. Sub Command2_Click()
  193.      .
  194.      .
  195.      .
  196.      hArray = HugeDim(len(i%), 10)
  197.      If hArray < 0 Then Stop
  198.      e% = SetHugeEl(hArray, 1, 54%)               ' puts 54 into element #1
  199.      If e% < 0 Then Stop                ' check error code
  200.      e% = GetHugeEl(hArray, 1, i%)           ' get element #1 into i%
  201.      Print i%
  202.  
  203.      e% = HugeErase(hArray)
  204. End Sub
  205.  
  206. -----------------------------------------------------------------------
  207. HugeInt:
  208. HugeLong:
  209. HugeSingle:
  210. HugeDouble:
  211. HugeCurrency:
  212.  
  213. Action:  Retrieves an element from an array of a given type.
  214.  
  215. Syntax:  HugeInt(hArray%, el&)
  216.          HugeLong(hArray%, el&)
  217.          HugeSingle(hArray%, el&)
  218.          HugeDouble(hArray%, el&)
  219.          HugeCurrency(hArray%, el&)
  220.  
  221. Argument       Description
  222. --------       -----------
  223.  
  224. hArray%        The handle to the array.
  225.  
  226. el&            The element to retrieve.
  227.  
  228. Remarks:
  229. --------
  230.  
  231. This family of functions is provided as a shortcut alternative to GetHugeEl
  232. to retrieve a given data type while in an expression.  For example:
  233.  
  234.      value = HugeDouble(hArray, 0) * HugeDouble(hArray, 1)
  235.  
  236. The example above could have been done using GetHugeEl; however, the values
  237. returned by the two HugeDouble calls would have to be assigned to
  238. variables, and then the variables would be used in the expression.  The
  239. example below expands more on this.
  240.  
  241. IMPORTANT: None of these functions return error codes, so you should use
  242. them only if you are positive that the value of hArray% and el& are legal
  243. values.  If a error does occur (such as a "Subscript Out of Range"), you
  244. will get meaningless results.  You should use GetHugeEl if you need to be
  245. able to check error codes.
  246.  
  247. Example:
  248. --------
  249.  
  250. Sub Command3_Click()
  251.      Dim hArray As Integer
  252.  
  253.      hArray = HugeDim(len(i%), 10)
  254.      If hArray < 0 Then Stop
  255.  
  256.      e% = SetHugeEl(hArray, 0, 3%)           ' Put 3 in element #0
  257.      If e% < 0 Then Stop                     ' Check for errors
  258.      e% = SetHugeEl(hArray, 1, 4%)           ' Put 4 in element #1
  259.      If e% < 0 Then Stop
  260.  
  261.      e% = GetHugeEl(hArray, 0, i%)           ' Get value of element #0
  262.      If e% < 0 Then Stop
  263.      e% = GetHugeEl(hArray, 1, j%)           ' Get value of element #1
  264.      If e% < 0 Then Stop
  265.      Print Sqr(i% ^ 2 + j ^ 2)
  266.  
  267.                                              ' Alternate (and faster)
  268.                                              ' way of doing the above.
  269.      Print Sqr(HugeInt(hArray, 0) ^ 2 + HugeInt(hArray, 1) ^ 2)
  270.  
  271.      e% = HugeErase(hArray)
  272. End Sub
  273.  
  274. -------------------------------------------------------------------------
  275. HugeUbound:
  276.  
  277. Action:  Returns the upper bound of a give array.
  278.  
  279. Syntax:  HugeUbound(hArray%)
  280.  
  281. Argument       Description
  282. --------       -----------
  283.  
  284. hArray%        The handle to the array.
  285.  
  286. Remarks:
  287. --------
  288.  
  289. This function is the same as Basic's 'Ubound' function.  It is used to
  290. return the current upper bound of an array.
  291.  
  292. If HugeUbound returns an negative value, it represents an error code.
  293.  
  294. Example:
  295. --------
  296.  
  297. Sub Command4_Click()
  298.      Dim hArray as integer
  299.  
  300.      hArray = HugeDim(len(i%), 23)
  301.      If hArray < 0 Then Stop
  302.  
  303.      for J& = 0 to HugeUbound(hArray)           ' Initialize array to 10's
  304.           ErrCode% = SetHugeEl(hArray, J&, 10%)
  305.           If ErrCode% < 0 Then Stop
  306.      Next J&
  307.      .
  308.      .
  309.      .
  310. End Sub
  311.  
  312. ------------------------------------------------------------------------
  313. NumHugeArrays:
  314.  
  315. Action:  Returns the number of free huge arrays available.
  316.  
  317. Syntax:  NumHugeArrays
  318.  
  319. Remarks:
  320. -------
  321.  
  322. This command is included mostly for debugging purposes.  It is used to find
  323. out how many array could be dimensioned at that time by the program.
  324.  
  325. Example:
  326. --------
  327.  
  328. Sub Command5_Click()
  329.  
  330.      Debug.Print NumHugeArrays
  331.  
  332. End Sub
  333.  
  334. ------------------------------------------------------------------------
  335. HugeSave:
  336.  
  337. Action:  Saves the specified number of elements of a huge array to the
  338.          named file.
  339.  
  340. Syntax:  HugeSave(hArray%, NEl&, RecLen%, Fn$)
  341.  
  342. Remarks:
  343. -------
  344.  
  345. The number of elements to save as well as the size of each element,
  346. together with the full filename of the file to be written to must be
  347. spupplied.
  348.  
  349. Example:
  350. --------
  351.  
  352. Sub Command6_Click()
  353.  
  354.        ErrCode& = HugeSave(hArray, hArrayElements&, hArryRecLen%, "MYFILE.ARR")
  355.        If ErrCode& < 0 Then Stop
  356.  
  357. End Sub
  358.  
  359. ---------------------------------------------------------------------------
  360. HugeLoad:
  361.  
  362. Action:  Loads a huge array from the named file.
  363.  
  364. Syntax:  HugeLoad(hArray%, RecLen%, Fn$)
  365.  
  366. Remarks:
  367. -------
  368.  
  369. The file is read until EOF is found.  The number of element read are
  370. returned unless an error occurs.  The Array to be loaded must be
  371. allocated sufficient space to hold the array before this function is
  372. called.
  373.  
  374. Example:
  375. --------
  376.  
  377. Sub Command7_Click()
  378.  
  379.        Open "MYFILE.ARR" For Random As #1 Len = hArrayRecLen%
  380.        hArrayElements& = LOF(1) / hArrayRecLen%
  381.        Close #1
  382.        hArray = HugeDim(hArrayRecLen%, hArrayElements&)
  383.        If hMain > 0 Then
  384.          hArrayElements& = HugeLoad(hArray, hArrayRecLen%, "MYFILE.ARR")
  385.          If hArrayElements& < 0& Then Stop
  386.        Else
  387.          Stop
  388.        End If
  389.  
  390. End Sub
  391.  
  392. ---------------------------------------------------------------------------
  393. Error Codes:
  394.  
  395. The following functions return error codes as described below:
  396.  
  397.      HugeDim, HugeErase, HugeRedim, GetHugeEl, SetHugeEl, HugeUbound,
  398.      HugeSave, HugeLoad
  399.  
  400. The possible error codes are:
  401.  
  402.      0    The function was successful.
  403.  
  404.      -1   "Out of Memory"  There is not enough global memory for the
  405.           HugeDim or HugeRedim
  406.  
  407.      -2   "To Many Arrays"  There are no free arrays left to be
  408.           dimensioned.
  409.  
  410.      -3   "Bad Element Size"  The array that you are trying to dimension is
  411.           greater than 64K, and the element size is not an integer power of
  412.           2.
  413.  
  414.      -4   "Subscript out of Range"  The array element you are trying to
  415.           access is outside the bounds of the array.
  416.  
  417.      -5   "Illegal Array Handle"  The array that was referenced is not a
  418.           valid array.  Either it is not dimensioned, or the handle value
  419.           is illegal.
  420.  
  421.      -7   "Error Opening File"  The file specified could not be opened.
  422.  
  423.      -8   "Error Writing to File"  An error occurred during a file write
  424.           operation
  425.  
  426.      -9   "Error Reading File"  An error occurred during a file read
  427.           operation
  428.  
  429. ------------------------------------------------------------------------
  430. Aliasing:
  431.  
  432. The GetHugeEl and SetHugeEl functions transfer data back and forth from an
  433. array. Because these functions must work with any data type, they are
  434. declared 'As Any' in their declarations.  This has the disadvantage that it
  435. defeats Basic's built-in type checking, and allows the posibility of
  436. passing the wrong data type.
  437.  
  438. To work around this potential problem, you can use Basic's 'Alias' keyword
  439. in the declaration.  For example, if you wanted GetHugeEl to work with the
  440. data type 'UserType', you might rename the function to 'GetUserType', and
  441. alias it to 'GetHugeEl'.
  442.  
  443. The declaration for GetHugeEl contained in "HUGEARR.BAS"  is:
  444.  
  445.      Declare Function GetHugeEl Lib "hugearr.dll" (ByVal Index%,
  446.      ByVal el&, buffer As Any) As Integer
  447.  
  448. To force Basic to do type checking on the call, you would rewrite the
  449. declaration to be:
  450.  
  451.      Declare Function GetUserType Lib "hugearr.dll" Alias "GetHugeEl"
  452.      (ByVal Index%, ByVal el&, buffer As UserType) As Integer
  453.  
  454. Note that the 'buffer As Any' has been changed to 'buffer As UserType'.
  455. Because the function no longer has the 'As Any' type Basic will be able to
  456. raise an error if you try to pass the wrong data type.
  457.  
  458. A function can be aliased any number of times, so you may want to create an
  459. alias for each data type that you are using.
  460.  
  461. -----------------------------------------------------------------------
  462. Constants:
  463.  
  464. The SetHugeEl routine is used to assign values to an array.  For example,
  465. the following statement
  466.  
  467.      i% = SetHugeEl(hArray, 1, v%)
  468.  
  469. would assign the value stored in v% to element #1 of the array hArray.
  470. However, a problem can arise when you try to use a constant in place of the
  471. 'v%' above.  For example:
  472.  
  473.      i% = SetHugeEl(hArray, 1, 15)
  474.  
  475. In this case, Visual Basic would assume that the number 15 is an integer
  476. constant and would pass a pointer to an integer to the SetHugeEl routine;
  477. even if hArray is a (for instance) array of double-precision numbers.
  478. To work around this potential problem, you should always use a type suffix
  479. when passing constants to SetHugeEl.  If you wanted to assign the value
  480. of 15 to a double precision array, you would use the statement:
  481.  
  482.      i% = SetHugeEl(hArray, 1, 15#)
  483.  
  484. --------------------------------------------------------------------------
  485.  
  486. HUGEARR.DLL Memory and Capacity:
  487.  
  488. HUGEARR.DLL allocates memory using the Windows API function GlobalAlloc.
  489. This means that the largest array that can be allocated is 1 MB in standard
  490. mode, and 64 MB in 386 enhanced mode.
  491.  
  492. If you forget to HugeErase an array that was allocated with HugeDim,
  493. Windows will automatically deallocate the memory after your application
  494. terminates.  However, HUGEARR.DLL keeps the information it needs to
  495. maintain the arrays in it's own private area.  This means that any array
  496. which is not HugeErase'd will not have it's information released, and the
  497. array will not be marked as free.  If two applications are both using the
  498. DLL, and the first application HugeDim's all of the arrays and then quits
  499. without HugeEraseing them, the second application will not be able to
  500. create any arrays.
  501.  
  502. --------------------------------------------------------------------------
  503.  
  504. References:
  505.  
  506. HUGEARR.DLL is written in Microsoft C, and the C source code is
  507. provided with this application note in HUGEARR.C and HUGEARR.H.
  508. Advanced programmers can optionally modify and rebuild HUGEARR.DLL, by
  509. using Microsoft C Compiler version 6.00 or 6.00a and DLL libraries
  510. from the Microsoft Windows 3.0 Software Development Kit (SDK), and by
  511. running NMAKE.EXE with the enclosed MAKEFILE.  The MAKEFILE tells
  512. LINK.EXE to use the enclosed linker definition file, HUGEARR.DEF.
  513.  
  514. The following references discuss how to program Windows 3.0 DLL
  515. routines:
  516.  
  517. 1. "Programming Windows: the Microsoft Guide to Writing Applications
  518.    for Windows 3," by Charles Petzold (published by Microsoft Press,
  519.    1990)
  520.  
  521. 2. Microsoft Windows 3.0 Software Development Kit
  522.